package com.recipes.meg;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;

public class ViewRecipe extends Activity {
	// Declare variable to hold database helper and sqlite database
	SQLiteDatabase db;
	DatabaseHelper db_helper;
	
	// Declare variable to hold id of this recipe
	int id;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_recipe);
        
        // Instantiate new database helper
        db_helper = new DatabaseHelper(getApplicationContext());
        
        // Get id of this recipe from calling activity
        id = this.getIntent().getIntExtra("id", 1);
        
        // Instantiate title TextView
        TextView title = (TextView) findViewById(R.id.title);
        
        // Open the database for reading
        db = db_helper.getWritableDatabase();
        
        // Set the text of title to be the name of this recipe
        title.setText(db_helper.getName(db, id));
        
        // Close the database
        db.close();
        
        // Instantiate table layout inside scroll view defined in xml layout
        TableLayout ing_table = (TableLayout) findViewById(R.id.ing_table);
        
        // Open the database for reading
        db = db_helper.getWritableDatabase();
        
        // Get a list of ingredients for this recipe
        ArrayList<String> ingredients = db_helper.getIng(db, id);
    
        // Close the database
        db.close();
        
        // Iterate through ingredients array
        for(int i = 0; i < ingredients.size(); i++){
        	// Create a new row for the table
        	TableRow temp_row = new TableRow(this);
        	
        	// Create a TextView to display this ingredient
        	TextView temp_ing = new TextView(this);
        	
        	// Set the color of this text to be black
        	temp_ing.setTextColor(getResources().getColor(R.color.black));
        	
        	// Set the size of this text to be 15sp
        	temp_ing.setTextSize(15);
        	
        	// Set the text of temp_ing to be this ingredients name
        	temp_ing.setText(ingredients.get(i));
        	
        	// Declare the parameters to be used for this text
        	TableRow.LayoutParams temp_params1 = new TableRow.LayoutParams(
        			LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        	
        	// Set the top margin of this text to be 5
        	temp_params1.setMargins(0, 5, 0, 0);
        	
        	// Set the parameters of this text
        	temp_ing.setLayoutParams(temp_params1);
        	
        	// Add this text to this row
        	temp_row.addView(temp_ing);

        	// Add temp_row to the table layout
        	ing_table.addView(temp_row, new TableLayout.LayoutParams(
        			LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        }
        
        // Initialize instructions TextView
        TextView instructions_display = (TextView) findViewById(R.id.instructions);
        
        // Open the database for reading
        db = db_helper.getWritableDatabase();
        
        // Get the instructions for this recipe
        String instructions = db_helper.getInstructions(db, id);
        
        // Close the database
        db.close();
        
        // Check if instructions are blank
        if(instructions.equalsIgnoreCase("")){
        	// If so then replace it with error text
        	instructions = "Could not retrieve instructions from database.";
        }
        
        // Set the instructions text to display the instructions for this recipe
        instructions_display.setText(instructions);
        
        // Initialize edit button
        Button edit = (Button) findViewById(R.id.edit);
        
        // Set onClickListener for edit button
        edit.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Initialize intent to start Edit activity
				Intent start_edit = new Intent(getApplicationContext(), Edit.class);
				
				// Put id into start_edit intent as an extra
				start_edit.putExtra("id", id);
				
				// Start Edit activity using start_edit intent
				startActivity(start_edit);
				
				// When edit activity returns finish this activity
				finish();
			}
		});
        
        // Initialize description button
        Button description = (Button) findViewById(R.id.description_button);
        
        // Set onClickListener for description
        description.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Initialize intent to start ViewDescription activity
				Intent start_description = new Intent(getApplicationContext(), ViewDescription.class);
			
				// Add id to start_description intent
				start_description.putExtra("id", id);
				
				// Start activity with start_description intent
				startActivity(start_description);
			}
		});
		
        // Initialize delete button
        Button delete = (Button) findViewById(R.id.delete);
        
        // Set onClickListener for delete button
        delete.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Open database for reading and writing
				db = db_helper.getWritableDatabase();
				
				// Call delete function with this id and save result
				int result = db_helper.deleteRecipe(db, id);
				
				// Close the database
				db.close();
				
				// Check if the result was -1
				if(result == -1){
					// If so then give an error message
					Toast.makeText(getApplicationContext(), "Could not delete recipe from database", Toast.LENGTH_SHORT).show();
				}
				
				// Else check if the result was -2
				else if(result == -2){
					// If so then give an error message
					Toast.makeText(getApplicationContext(), "Could not reorganize database after deletion. Database is currently in a fractured state. Uninstall app and re-install it", Toast.LENGTH_SHORT).show();
				}
				
				// Else check if result was -3
				else if(result == -3){
					// If so then give an error message
					Toast.makeText(getApplicationContext(), "Last recipe of database could not be deleted and is now present twice. Try deleting it manually", Toast.LENGTH_SHORT).show();
				}
				
				// Finish this activity
				finish();
			}
		});
    }
}
